package Longing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class demo3 {
    /**
     * @Author 枫城
     * @Date 2022/4/5
     * @Param 完成prepareStatement的增删改查
     **/
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement ps = null;
        String url = "jdbc:mysql://192.168.10.132:3306/learn_JDBC?useUnicode=true&characterEncoding=utf-8";
        String user = "root";
        String password = "123456";
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(url,user,password);

            String sql = "insert into user(user_name, pwd, user_rename) values(?,?,?)";
            ps = connection.prepareStatement(sql);
            Scanner s = new Scanner(System.in);

            System.out.print("昵称：");
            String na = s.nextLine();

            System.out.print("密码：");
            String pa = s.nextLine();

            System.out.print("真实姓名：");
            String re = s.nextLine();

            ps.setString(1,na);
            ps.setString(2,pa);
            ps.setString(3,re);

            int count = ps.executeUpdate();
            System.out.println(count);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (ps != null){
                try{
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null){
                try{
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
